//
//  RepeatLoginController.m
//  NPS
//
//  Created by jadd on 15/11/3.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "RepeatLoginController.h"

#define fitScreenHeight (K_ScreenHeight/568)
#define kName      0
#define kPass      1

@implementation RepeatLoginController

@synthesize userName,userPwd;
@synthesize tempValues,textFieldBeingEdited;


-(IBAction)backACtion:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"");
    }];
}

-(IBAction)toLogin:(id)sender
{
    
    if (textFieldBeingEdited != nil)
    {
        NSNumber *tagAsNum= [[NSNumber alloc] initWithInteger:textFieldBeingEdited.tag];
        [tempValues setObject:textFieldBeingEdited.text forKey: tagAsNum];
        
    }
    
    NSString *loginId = nil;
    NSString *loginPwd = nil;
    
    for (NSNumber *key in [tempValues allKeys])
    {
        switch ([key intValue]) {
            case kName:
                loginId = [tempValues objectForKey:key];
                break;
            case kPass:
                loginPwd = [tempValues objectForKey:key];
                break;
            default:
                break;
        }
    }
    
    if ([loginId isEqualToString:@""]){
        [Config showMessage:@"用户名不能为空！"];
    }else if([loginPwd isEqualToString:@""]) {
        [Config showMessage:@"密码不能为空！"];
    }else{
        [textFieldBeingEdited resignFirstResponder];
      
        //[Config showActivityView];
        LoginRequest * request = [[LoginRequest alloc] initWithUserName:loginId
                                                               password:loginPwd];
        
        [RequestHandle startOperationWithRequest:request
                                        complete:^(id responseObject) {
                                            
                                            LoginResponse * response = [[LoginResponse alloc] init];
                                            response.responseObject = responseObject; //保存登陆数据
                                            
                                        
                                            while (![[NSUserDefaults standardUserDefaults] synchronize]) {}
                                            //登陆成功
                                            [Config hideActivityView];
                                            [self dismissViewControllerAnimated:YES completion:^{
                                                NSLog(@"");
                                            }];
                                            [MyGlobal sharedVariables].needLoad = YES;
                                            
                                        } error:^(NSError *error) {
                                            
                                           // [Config hideActivityView];
                                            [Config showMessage:error.localizedDescription];
                                            
                                        }];
        
    }
}

-(void)setupButton
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame = CGRectMake(0, 0, K_Global_BackButton_Width, K_Global_BackButton_Height);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backACtion:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIBarButtonItem *loginBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(toLogin:)];
    
    self.navigationItem.rightBarButtonItem = loginBtn;

    
}

-(void) setupView
{
    UIImageView *backView = [[UIImageView alloc]init];
    backView.userInteractionEnabled = YES;
    [backView setImage:[UIImage imageNamed:@"TextFieldBg"]];
    [self.view addSubview:backView];
    
    //top left bottom right
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.height.equalTo(@150);
        make.top.offset(80*fitScreenHeight);
        make.centerX.equalTo(self.view.mas_centerX);
        
    }];
    
    //账号
    UILabel *Account = [[UILabel alloc] init];
    [Account setText:@"用户名"];
    [Account setTextColor:DARKGRAY];
    Account.adjustsFontSizeToFitWidth = YES;
    Account.font = [UIFont boldSystemFontOfSize:17];
    [backView addSubview:Account];
    
    [Account mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.top.offset(15);
        make.left.offset(10);
    }];
    


    userName = [[UITextField alloc] init];
    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_uName];
    bool hasLogin = (uid == nil || [uid isEqualToString:@""]);
    if(hasLogin == NO)
    {
        userName.text =uid;
    }
    NSNumber *rowAsNum = [[NSNumber alloc] initWithInteger:0];
    [tempValues setObject:userName.text forKey:rowAsNum];
    //userName.borderStyle = UITextBorderStyleRoundedRect;
    userName.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
    userName.clearButtonMode = UITextFieldViewModeWhileEditing;
    userName.font = [UIFont systemFontOfSize:18];
    userName.textColor = GRAY;
    userName.delegate = self;
    userName.tag = 0;
    userName.placeholder = @"手机/邮箱";
    [backView addSubview:userName];
    
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Account.mas_top).with.offset(4);
        make.left.mas_equalTo(Account.mas_right).with.offset(10);
        make.right.mas_equalTo(backView.mas_right).with.offset(-10);
    }];
    
    
    ////密码
    UILabel *password = [[UILabel alloc] init];
    rowAsNum = [[NSNumber alloc] initWithInteger:1];
    [tempValues setObject:@"" forKey:rowAsNum];
    [password setText:@"密 码"];
    [password setTextColor:DARKGRAY];
    password.adjustsFontSizeToFitWidth = YES;
    password.font = [UIFont boldSystemFontOfSize:17];
    [backView addSubview:password];
    
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.left.mas_equalTo(Account.mas_left);
        make.top.mas_equalTo(Account.mas_bottom).with.offset(15);
    }];


    self.userPwd = [[UITextField alloc] init];
    self.userPwd.delegate = self;
    self.userPwd.tag = 1;
    self.userPwd .clearButtonMode = UITextFieldViewModeWhileEditing;
    self.userPwd.secureTextEntry = YES;
    self.userPwd.font = [UIFont systemFontOfSize:18];
    self.userPwd.textColor = GRAY;
    [backView addSubview:self.userPwd];
  
    [self.userPwd  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(password.mas_top).with.offset(4);
   
        make.left.mas_equalTo(password.mas_right).with.offset(10);
        make.right.mas_equalTo(backView.mas_right).with.offset(-10);
    }];

}


- (void)viewDidLoad {
    
    self.title = @"重新登录";
    self.view.backgroundColor = BACK_VIEW_COLOR;
    
    tempValues = [[NSMutableDictionary alloc] init];
    [self setupView];
    [self setupButton];
    [super viewDidLoad];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = false;
}

-(void)viewDidAppear:(BOOL)animated
{
    [Config showMessage:@"登录过期，请重新登录!"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.textFieldBeingEdited = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSNumber *tagAsNum = [[NSNumber alloc] initWithInteger:textField.tag];
    if(textField.text != nil){
        [tempValues setObject:textField.text forKey:tagAsNum];
    }}


@end
